<?php

	function flickr_query($method, array $param = NULL)
	{
		global $CONFIG;
		global $FLICKR_QUERY_CACHE;
		
		if (!$FLICKR_QUERY_CACHE) 
			$FLICKR_QUERY_CACHE = array();
		
		$endpoint = "http://api.flickr.com/services/rest/";
		
		if (!$param)
			$param = array();
			
		$param['method'] = $method;
		$param['api_key'] = $CONFIG->flickr_token;
		$param['format'] = 'php_serial';
		
		$p2 = array();
		foreach ($param as $k => $v)
			$p2[] = "$k=" . urlencode($v);
		
		$url = "$endpoint?". implode('&', $p2);
		
		if (isset($FLICKR_QUERY_CACHE[md5($url)]))
			return $FLICKR_QUERY_CACHE[md5($url)];
		
		$result = file_get_contents($url);
	
		if ($result) {
			$result = unserialize($result);
			$FLICKR_QUERY_CACHE[md5($url)] = $result;
			
			return $result;
		}

		return false;
	}

	function is_nsid($username)
	{
		if (strpos($username, "@")!== false)
			return true;
			
		return false;
	}

	function get_username_from_url_flickr($url)
	{
/*		$url = trim($url, " /");

		$username = substr($url, strrpos($url, "/")+1);
		if (is_nsid($username))
			return $username;
			
		return get_nsid_from_username($username);*/
		
		$result = flickr_query('flickr.urls.lookupUser', array('url' => $url));
		
		if ($result) {
			return $result['user']['id'];
		}
			
		return false;
	}

	function get_friends_flickr($url)
	{
		$username = get_username_from_url('flickr', $url);
		if ($username)
		{
			
		}
		
		return false;
	}
	
	function get_tags_flickr($url)
	{
	
	}
	
	function get_header_flickr()
	{
		global $CONFIG;
		return <<< END
			<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key={$CONFIG->google_maps_token}" type="text/javascript"></script>
		<script type="text/javascript">
			var maps = new Array();
			function mapInit() {
				var map_divs = document.getElementsByTagName('DIV');
				for (i = 0; i < map_divs.length; i ++) {
					if (map_divs[i].className == "map" && map_divs[i].id != "map_") {
						for (j = 0; j < maps.length; j ++) {
							if (maps[j][0] == map_divs[i].id) {
								mapload(maps[j]);
							}							
						}
					}
				}
			}
			function mapload(mapData) {
				if  (GBrowserIsCompatible()) {
					var map = new GMap2(document.getElementById(mapData[0]));
					map.setCenter(new GLatLng(mapData[1],mapData[2]), 15);
					var point = new GLatLng(mapData[1],mapData[2]);
					map.addOverlay(new GMarker(point));
				}
			}
			onload = mapInit;
			onunload = GUnload;
		</script>
END;
	}
	
	function get_location_flickr($url)
	{
		$username = get_username_from_url('flickr', $url);
		if ($username)
		{
			
		}
		
		return false;
	}
	
	function get_user_icon_flickr($url)
	{	
		
		$username = get_username_from_url('flickr', $url);

		if ($username)
		{
			$result = flickr_query('flickr.people.getInfo', array('user_id' => $username));
			
			return "http://farm{$result['person']['iconfarm']}.static.flickr.com/{$result['person']['iconserver']}/buddyicons/{$result['person']['nsid']}.jpg";
			
		}
		
		return 'http://www.flickr.com/images/buddyicon.jpg';
		
	}
	
	function get_geo_from_photo_guid($guid)
	{
		$photo_id = substr($guid, strrpos($guid, "/")+1);
		$result = flickr_query('flickr.photos.geo.getLocation', array('photo_id' => $photo_id));
	
		if ($result['stat']!='fail') {
			$return = array();
			$return['lat'] = $result['photo']['location']['latitude'];
			$return['long'] = $result['photo']['location']['longitude'];
			$return['photo_id'] = $photo_id;
		
			return $return;
		}
		
		return false;
	}
	
	function get_rendered_contents_flickr($url)
	{
		$sg = socialgraph_query($url);

		if (isset($sg->nodes->$url))
		{
			$return = "";
			$node = $sg->nodes->$url;

			if (isset($node->attributes->rss))
			{
			
				$rss = fetch_rss($node->attributes->rss);
				$n = 0;

				if(!$LAST_UPDATED) $LAST_UPDATED = array();
				$LAST_UPDATED[$url] = 0;
				
				foreach ($rss->items as $node)
				{
					$t = strtotime($node['pubdate']);
					if ($t>$LAST_UPDATED[$url]) $LAST_UPDATED[$url]=$t;
					
					if ($n<15) {
						if (strcmp($node['title'], $node['description'])!=0) 
							$desc = "<div id=\"description\">{$node['description']}</div>";
						else
							$desc = "";
							
						$geo_gumph="";
						$geo = get_geo_from_photo_guid($node['guid']);
						if ($geo) {
							$geo_gumph = <<< END
							<td>
						<script type="text/javascript">
							var map_data = new Array("map_{$geo['photo_id']}", {$geo['lat']}, {$geo['long']});
							maps.push(map_data);
						</script>
						<div class="map" style="width: 180px; height: 240px" id="map_{$geo['photo_id']}"/>
						</td>
END;
						}
							$return .= <<< END
							<div class="rss_item">
						<table>
							<tr>
								<td width="100%">
									
										<div id="title">{$node['title']}</div>
										$desc
										<div id="date">{$node['pubdate']}</div>
									
								</td>
								
									$geo_gumph
							</tr>
						</table>
						</div>

END;
						$n++;
					}
				}
			}
			
			return $return;
		}

		return false;
	}
?>
